#include <a_samp>
#include <zcmd>


#define COLOR_CYAN 		0x00FFFFAA
#define COLOR_LIGHTRED  0xFF6347AA
#define COLOR_BYELLOW   0xFFFF00AA
#define COLOR_WHITE     0xFFFFFFAA

#define HOLDING(%0) \
	((newkeys & (%0)) == (%0))

#define PRESSED(%0) \
	(((newkeys & (%0)) == (%0)) && ((oldkeys & (%0)) != (%0)))

#define RELEASED(%0) \
	(((newkeys & (%0)) != (%0)) && ((oldkeys & (%0)) == (%0)))

new spotlightob[MAX_PLAYERS];
new spotlight[MAX_PLAYERS][3];
new spotlighton[MAX_PLAYERS];
new lighton[MAX_PLAYERS];
new Float:spotlightpos[MAX_PLAYERS][3];
new spotlighttimer[MAX_PLAYERS];
new name[MAX_PLAYER_NAME];

public OnFilterScriptInit()
{
	print("\n--------------------------------------");
	print(" Spotlights for Patriots   ");
	print("--------------------------------------\n");
	
	for(new i = 0; i < MAX_PLAYERS; i ++)
	{
	    if(IsPlayerConnected(i))
	    {
	        spotlighton[i] = 0;
	       	lighton[i] = 0;
			spotlightob[i] = 18656;
		}
	}
	
	
	return 1;
}

public OnFilterScriptExit()
{
    for(new i = 0; i < MAX_PLAYERS; i ++)
	{
	    if(IsPlayerConnected(i))
	    {
	        if(spotlighton[i] == 1)
	        {
	            DestroyObject(spotlight[i][0]);
			    DestroyObject(spotlight[i][1]);

				if(lighton[i] == 1)
			    	DestroyObject(spotlight[i][2]);
	        }
		}
	}
	return 1;
}

public OnPlayerConnect(playerid)
{
    spotlighton[playerid] = 0;
    lighton[playerid] = 0;
	spotlightob[playerid] = 18656;
	return 1;
}

public OnPlayerDisconnect(playerid, reason)
{
	if(spotlighton[playerid] == 1)
	{
	    DestroyObject(spotlight[playerid][0]);
	    DestroyObject(spotlight[playerid][1]);
	    spotlighton[playerid] = 0;
	    
	    if(lighton[playerid] == 1)
	    {
			DestroyObject(spotlight[playerid][2]);
			lighton[playerid] = 0;
		}
	}
	return 1;
}

forward CheckingSpotlight(playerid,carid,position);
public CheckingSpotlight(playerid,carid,position)
{
	if(position == 0)
	{
	    if(spotlightpos[playerid][1] < -170.0)
	        return 1;

  		spotlightpos[playerid][1] -= 50.0;
		spotlightpos[playerid][0] += 0.02;
		AttachObjectToVehicle(spotlight[playerid][0],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		AttachObjectToVehicle(spotlight[playerid][1],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);

		if(lighton[playerid] == 1)
			AttachObjectToVehicle(spotlight[playerid][2],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
	}
	else if(position == 1)
	{
		if(spotlightpos[playerid][1] > -2.0)
	        return 1;
	        
		spotlightpos[playerid][1] += 50.0;
		spotlightpos[playerid][0] -= 0.02;
		AttachObjectToVehicle(spotlight[playerid][0],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		AttachObjectToVehicle(spotlight[playerid][1],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);

        if(lighton[playerid] == 1)
			AttachObjectToVehicle(spotlight[playerid][2],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
	}
	else if(position == 2)
	{
		spotlightpos[playerid][2] += 4.0;
		AttachObjectToVehicle(spotlight[playerid][0],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		AttachObjectToVehicle(spotlight[playerid][1],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);

        if(lighton[playerid] == 1)
			AttachObjectToVehicle(spotlight[playerid][2],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
	}
	else if(position == 3)
	{
		spotlightpos[playerid][2] -= 4.0;
		AttachObjectToVehicle(spotlight[playerid][0],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		AttachObjectToVehicle(spotlight[playerid][1],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);

        if(lighton[playerid] == 1)
			AttachObjectToVehicle(spotlight[playerid][2],carid,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
	}
	return 1;
}

public OnPlayerKeyStateChange(playerid, newkeys, oldkeys)
{
    new car = GetPlayerVehicleID(playerid);

	if( PRESSED(KEY_CROUCH) )
	{
	    if(IsPlayerInAnyVehicle(playerid))
	    {
	        if(IsModelAPatriot(car))
	        {
	            if(spotlighton[playerid] == 1)
	            {
		            if(lighton[playerid] == 0)
					{
					    spotlight[playerid][2] = CreateObject(spotlightob[playerid],0,0,0,0,0,0);

						AttachObjectToVehicle(spotlight[playerid][2],car,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
						lighton[playerid] = 1;
					}
					else
					{
					    DestroyObject(spotlight[playerid][2]);
					    lighton[playerid] = 0;
					}
				}
			}
		}
	}

    if(spotlighton[playerid] == 1)
	{
		if (PRESSED(KEY_ANALOG_DOWN))
		{
			CheckingSpotlight(playerid, car, 0);
		}
		if ( HOLDING(KEY_ANALOG_DOWN) )
		{
		    if(spotlighttimer[playerid] == 0)
		    {
		        CheckingSpotlight(playerid, car, 0);
		    	spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,0);
			}
			else
			{
				KillTimer(spotlighttimer[playerid]);
				CheckingSpotlight(playerid, car, 0);
				spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,0);
			}
		}
		if ( RELEASED(KEY_ANALOG_DOWN) )
		{
			KillTimer(spotlighttimer[playerid]);
			spotlighttimer[playerid] = 0;
		}

		if (PRESSED(KEY_ANALOG_UP))
		{
		    CheckingSpotlight(playerid, car, 1);
		}
		if ( HOLDING(KEY_ANALOG_UP) )
		{
		    if(spotlighttimer[playerid] == 0)
		    {
		        CheckingSpotlight(playerid, car, 1);
		    	spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,1);
			}
			else
			{
				KillTimer(spotlighttimer[playerid]);
				CheckingSpotlight(playerid, car, 1);
				spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,1);
			}
		}
		if ( RELEASED(KEY_ANALOG_UP) )
		{
			KillTimer(spotlighttimer[playerid]);
			spotlighttimer[playerid] = 0;
		}


		if (PRESSED(KEY_ANALOG_LEFT))
		{
		    CheckingSpotlight(playerid, car, 2);
		}
		if ( HOLDING(KEY_ANALOG_LEFT) )
		{
		    if(spotlighttimer[playerid] == 0)
		    {
		        CheckingSpotlight(playerid, car, 2);
		    	spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,2);
			}
			else
			{
				KillTimer(spotlighttimer[playerid]);
				CheckingSpotlight(playerid, car, 2);
				spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,2);
			}
		}
		if ( RELEASED(KEY_ANALOG_LEFT) )
		{
			KillTimer(spotlighttimer[playerid]);
			spotlighttimer[playerid] = 0;
		}

		if (PRESSED(KEY_ANALOG_RIGHT))
		{
		    CheckingSpotlight(playerid, car, 3);
		}
		if ( HOLDING(KEY_ANALOG_RIGHT) )
		{
		    if(spotlighttimer[playerid] == 0)
		    {
		        CheckingSpotlight(playerid, car, 3);
		    	spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,3);
			}
			else
			{
				KillTimer(spotlighttimer[playerid]);
				CheckingSpotlight(playerid, car, 3);
				spotlighttimer[playerid] = SetTimerEx("CheckingSpotlight",50,true,"iii",playerid,car,3);
			}
		}
		if ( RELEASED(KEY_ANALOG_RIGHT) )
		{
			KillTimer(spotlighttimer[playerid]);
			spotlighttimer[playerid] = 0;
		}
	}
	return 1;
}

COMMAND:spotlightp(playerid,params[])
{
    if(!IsPlayerInAnyVehicle(playerid))
        return Error(playerid, "You're not in a Vehicle!");
        
	new car = GetPlayerVehicleID(playerid);
	
	if(!IsModelAPatriot(car))
       return Error(playerid, "Must be done on a patriot!");

	if(spotlighton[playerid] == 0)
	{
	    spotlight[playerid][0] = CreateObject(2888,0,0,0,0,0,0);
	    spotlight[playerid][1] = CreateObject(2887,0,0,0,0,0,0);

	    AttachObjectToVehicle(spotlight[playerid][0],car,0,0,2.3,50,0,0);
	    AttachObjectToVehicle(spotlight[playerid][1],car,0,0,2.3,50,0,0);

		spotlighton[playerid] = 1;
		spotlightpos[playerid][1] = -50.0;
		spotlightpos[playerid][0] = -2.3;
		spotlightpos[playerid][2] = 0;
		
		Info(playerid, "Spotlight Added! If you wish to turn it on use the Horn key!");
	}
	else
	{
	    DestroyObject(spotlight[playerid][0]);
	    DestroyObject(spotlight[playerid][1]);
	    DestroyObject(spotlight[playerid][2]);
	    spotlighton[playerid] = 0;
	}
	return 1;
}

COMMAND:newcommandsp(playerid,params[])
{
    SendClientMessage(playerid, COLOR_WHITE,"{FF66FF}Commands: {FFFFFF}/spotlightp(colorp)");
	return 1;
}

COMMAND:spotlightcolorp(playerid, params[])
{
    if(!IsPlayerInAnyVehicle(playerid))
        return Error(playerid, "You're not in a Vehicle!");

	new car = GetPlayerVehicleID(playerid);

    if(spotlighton[playerid] == 0)
		return Error(playerid,"Spot light must be on the vehicle!");

	new color[24];
	if(sscanf(params,"s",color))
	    return Usage(playerid, "/spotlightcolorp <red/white/blue>");

	if(!strcmp(color,"red"))
	{
	    spotlightob[playerid] = 18657;
	    DestroyObject(spotlight[playerid][2]);
	    spotlight[playerid][2] = CreateObject(spotlightob[playerid],0,0,0,0,0,0);
	    if(lighton[playerid] == 1)
	    	AttachObjectToVehicle(spotlight[playerid][2],car,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
	    SendClientMessage(playerid, COLOR_CYAN,"Spotlight will now be {FF0000}Red{00FFFF}!");
	}
	else if(!strcmp(color,"blue"))
	{
	    spotlightob[playerid] = 18658;
	    DestroyObject(spotlight[playerid][2]);
	    spotlight[playerid][2] = CreateObject(spotlightob[playerid],0,0,0,0,0,0);
	    if(lighton[playerid] == 1)
	    	AttachObjectToVehicle(spotlight[playerid][2],car,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		SendClientMessage(playerid, COLOR_CYAN,"Spotlight will now be {0000FF}Blue{00FFFF}!");
	}
	else if(!strcmp(color,"white"))
	{
	    spotlightob[playerid] = 18656;
	    DestroyObject(spotlight[playerid][2]);
	    spotlight[playerid][2] = CreateObject(spotlightob[playerid],0,0,0,0,0,0);
	    if(lighton[playerid] == 1)
	    	AttachObjectToVehicle(spotlight[playerid][2],car,0,0,spotlightpos[playerid][0],spotlightpos[playerid][1],0,spotlightpos[playerid][2]);
		SendClientMessage(playerid, COLOR_CYAN,"Spotlight will now be {FFFFFF}White{00FFFF}!");
	}
	return 1;
}

stock Error(playerid, const message[])
{
	new string[128];
	format(string, sizeof(string),"ERROR: {FFFFFF}%s",message);
	SendClientMessage(playerid,COLOR_LIGHTRED,string);
	return 1;
}
stock Info(playerid, const message[])
{
	new string[128];
	format(string, sizeof(string),"INFO: {FFFFFF}%s",message);
	SendClientMessage(playerid,COLOR_CYAN,string);
	return 1;
}
stock Usage(playerid, const message[])
{
	new string[128];
	format(string, sizeof(string),"USAGE: {FFFFFF}%s",message);
	SendClientMessage(playerid,COLOR_BYELLOW,string);
	return 1;
}


stock IsModelAPatriot(carid)
{
    new modelid = GetVehicleModel(carid);
	if(modelid == 470)
	{
		return 1;
	}
	return 0;
}

stock sscanf(string[], format[], {Float,_}:...)
{
	#if defined isnull
		if (isnull(string))
	#else
		if (string[0] == 0 || (string[0] == 1 && string[1] == 0))
	#endif
		{
			return format[0];
		}
	#pragma tabsize 4
	new
		formatPos = 0,
		stringPos = 0,
		paramPos = 2,
		paramCount = numargs(),
		delim = ' ';
	while (string[stringPos] && string[stringPos] <= ' ')
	{
		stringPos++;
	}
	while (paramPos < paramCount && string[stringPos])
	{
		switch (format[formatPos++])
		{
			case '\0':
			{
				return 0;
			}
			case 'i', 'd':
			{
				new
					neg = 1,
					num = 0,
					ch = string[stringPos];
				if (ch == '-')
				{
					neg = -1;
					ch = string[++stringPos];
				}
				do
				{
					stringPos++;
					if ('0' <= ch <= '9')
					{
						num = (num * 10) + (ch - '0');
					}
					else
					{
						return -1;
					}
				}
				while ((ch = string[stringPos]) > ' ' && ch != delim);
				setarg(paramPos, 0, num * neg);
			}
			case 'h', 'x':
			{
				new
					num = 0,
					ch = string[stringPos];
				do
				{
					stringPos++;
					switch (ch)
					{
						case 'x', 'X':
						{
							num = 0;
							continue;
						}
						case '0' .. '9':
						{
							num = (num << 4) | (ch - '0');
						}
						case 'a' .. 'f':
						{
							num = (num << 4) | (ch - ('a' - 10));
						}
						case 'A' .. 'F':
						{
							num = (num << 4) | (ch - ('A' - 10));
						}
						default:
						{
							return -1;
						}
					}
				}
				while ((ch = string[stringPos]) > ' ' && ch != delim);
				setarg(paramPos, 0, num);
			}
			case 'c':
			{
				setarg(paramPos, 0, string[stringPos++]);
			}
			case 'f':
			{
				setarg(paramPos, 0, _:floatstr(string[stringPos]));
			}
			case 'p':
			{
				delim = format[formatPos++];
				continue;
			}
			case '\'':
			{
				new
					end = formatPos - 1,
					ch;
				while ((ch = format[++end]) && ch != '\'') {}
				if (!ch)
				{
					return -1;
				}
				format[end] = '\0';
				if ((ch = strfind(string, format[formatPos], false, stringPos)) == -1)
				{
					if (format[end + 1])
					{
						return -1;
					}
					return 0;
				}
				format[end] = '\'';
				stringPos = ch + (end - formatPos);
				formatPos = end + 1;
			}
			case 'u':
			{
				new
					end = stringPos - 1,
					id = 0,
					bool:num = true,
					ch;
				while ((ch = string[++end]) && ch != delim)
				{
					if (num)
					{
						if ('0' <= ch <= '9')
						{
							id = (id * 10) + (ch - '0');
						}
						else
						{
							num = false;
						}
					}
				}
				if (num && IsPlayerConnected(id))
				{
					setarg(paramPos, 0, id);
				}
				else
				{
					#if !defined foreach
						#define foreach(%1,%2) for (new %2 = 0; %2 < MAX_PLAYERS; %2++) if (IsPlayerConnected(%2))
						#define __SSCANF_FOREACH__
					#endif
					string[end] = '\0';
					num = false;
					id = end - stringPos;
					foreach (Player, playerid)
					{
						GetPlayerName(playerid, name, sizeof (name));
						if (!strcmp(name, string[stringPos], true, id))
						{
							setarg(paramPos, 0, playerid);
							num = true;
							break;
						}
					}
					if (!num)
					{
						setarg(paramPos, 0, INVALID_PLAYER_ID);
					}
					string[end] = ch;
					#if defined __SSCANF_FOREACH__
						#undef foreach
						#undef __SSCANF_FOREACH__
					#endif
				}
				stringPos = end;
			}
			case 's', 'z':
			{
				new
					i = 0,
					ch;
				if (format[formatPos])
				{
					while ((ch = string[stringPos++]) && ch != delim)
					{
						setarg(paramPos, i++, ch);
					}
					if (!i)
					{
						return -1;
					}
				}
				else
				{
					while ((ch = string[stringPos++]))
					{
						setarg(paramPos, i++, ch);
					}
				}
				stringPos--;
				setarg(paramPos, i, '\0');
			}
			default:
			{
				continue;
			}
		}
		while (string[stringPos] && string[stringPos] != delim && string[stringPos] > ' ')
		{
			stringPos++;
		}
		while (string[stringPos] && (string[stringPos] == delim || string[stringPos] <= ' '))
		{
			stringPos++;
		}
		paramPos++;
	}
	do
	{
		if ((delim = format[formatPos++]) > ' ')
		{
			if (delim == '\'')
			{
				while ((delim = format[formatPos++]) && delim != '\'') {}
			}
			else if (delim != 'z')
			{
				return delim;
			}
		}
	}
	while (delim > ' ');
	return 0;
}
